<?php

App::uses('AppController', 'Controller');

class UsersController extends AppController {
	public $name = 'Users';
	public $uses = array('Sisusers','Users');
	public $helpers = array('Session');


	public function sisusers(){
		$sisusers = $this->paginate('Sisusers');// $this->Newsletters->find('all');
		//var_dump($sisusers);
		$this->set('page_title',"Administradores");
		$this->set('sisusers',$sisusers);
	}
	

	public function index(){

		$users = $this->paginate('Users', array('Users.is_delete = 0'));// $this->Newsletters->find('all');
		//var_dump($users);
		$this->set('page_title',"Usuarios");
		$this->set('users',$users);	
	}

	public function view($username)
	{		
		$user = $this->Users->findByUsername($username);
		//var_dump($user);
		$this->set("user",$user);
		$this->set('page_title',"Perfil de Usuario");
	}


	public function login(){
		$this->layout = false;
		$data = $this->data;
		/*if(!empty($data))
		{
			var_dump($data);
			
			if (($data['Sisusers']['email']!='')&&($data['Sisusers']['password']!='')){
				$sisuser = $this->Sisusers->findByEmail($data['Sisusers']['email']);
				
				if(!empty($sisuser))
				{
					//var_dump($data['Sisusers']['password']);
					Security::setHash('blowfish');
					$salted = $sha1 = Security::hash($data['Sisusers']['password'], 'sha1', true);//Security::hash($data['Sisusers']['password'], '1234567', $sisuser['Sisusers']['password']);
					//$secret = Security::cipher('hello world', 'my_key');
					if($salted == $sisuser['Sisusers']['password'] ){
						var_dump($this->Auth->redirectUrl());
						return $this->redirect($this->Auth->redirectUrl());
					}

					
				}else{
					echo "ss";
					$this->Session->setFlash(__('Debe Ingresar Email'), 'error', array());//,'login_error')	;
					echo $this->Session->flash('login_error'); 
				}

			}else{
				$this->Session->setFlash(__('Ningun campo puede ser vacio'), 'error', array('class' => 'alert '),'login_error')	;
					//echo $this->Session->flash(); 
			}
			//$this->render(false);
		}*/
		if ($this->request->is('post')) 
		{
			
		if ($this->Auth->login($this->data['Sisusers'])) 
		{
			return $this->redirect($this->Auth->redirectUrl());
			// Prior to 2.3 use `return $this->redirect($this->Auth->redirect());`
		} else {

		$this->Session->setFlash(__('Usuario o Contraseña son incorrectos'), 'error', array(),'login_error')	;
		}
    	}

	}
	
	public function sislogout(){
		$this->Session->destroy();
		return $this->redirect(array('controller' => 'orders', 'action' => 'thanks'));
	}

	public function add(){
		$data = $this->data;
		if(!empty($data)){
			//var_dump($data);
			$this->Users->set($data);
			if($this->Users->validates()){
				if( $this->data[ 'Users' ][ 'password' ] != $this->data[ 'Users' ][ 're_password' ] )
				{
					$this->Users->invalidate( 're_password', "Los password no coinciden." );
				}
				if($this->Users->save($this->data))
				{

					$this->Session->setFlash(__("Se registro exitosamente nuevo usuario ".$this->data['Users']['username'], true), 'flash', array('class' => 'muted'));
					$this->redirect(array('action' => 'index'));
				}
				else{		
				$errors = $this->Users->validationErrors;
			    //var_dump($errors);			
				$this->Session->setFlash(__("No se pudo crear nuevo usuario, por favor revisar los campos requeridos.", true), 'flash_error', array('class' => 'alert alert-error'));
				}
			}
		}
		$this->set('page_title',"Nuevo Usuario");	
	}

	public function sisadd(){
		var_dump($this->data);
		$data_source = $this->data;
		if(!empty($data_source)){
			
			$data = array('Sisusers'=>$data_source['Users']);
			var_dump($data);
			$this->Sisusers->set($data);
			if($this->Sisusers->validates()){
				if( $data[ 'Sisusers' ][ 'password' ] != $data[ 'Sisusers' ][ 're_password' ] )
				{
					$this->Sisusers->invalidate( 're_password', "Los password no coinciden." );
				}
				if($this->Sisusers->save($data))
				{

					$this->Session->setFlash(__("Se registro exitosamente nuevo usuario ".$data['Sisusers']['username'], true), 'flash', array('class' => 'muted'));
					$this->redirect(array('action' => 'sisusers'));
				}
				else{		
				$errors = $this->Sisusers->validationErrors;
			    var_dump($errors);			
				$this->Session->setFlash(__("No se pudo crear nuevo usuario, por favor revisar los campos requeridos.", true), 'flash_error', array('class' => 'alert alert-error'));
				}
			}
		}
		$this->set('page_title',"Nuevo Administrador");	
	}

	public function edit($user_id){
		$data = $this->data;
		//var_dump($user_id);

		if(!empty($data))
		{
			//var_dump($data);
			$this->Users->id=$user_id;
			if($this->Users->save($data))
			{
				$this->Session->setFlash(__("Se Actualizo usuario ".$this->data['Users']['username'], true), 'flash', array('class' => 'muted'));
				$this->redirect(array('action' => 'index'));
			}
			else
			{		
				$errors = $this->Users->validationErrors;
			    //var_dump($errors);			
				$this->Session->setFlash(__("No se pudo crear nuevo usuario, por favor revisar los campos requeridos.", true), 'flash_error', array('class' => 'alert alert-error'));
			}			
		}

		$user = $this->Users->findById($user_id);
		$this->data = $user;
		$this->set('page_title',"Nuevo Usuario");	
	}

	public function delete($user_id)
	{		
		$this->Users->id=$user_id;
		if($this->Users->saveField('is_delete',1))
		{
			$this->Session->setFlash(__("Se Elimino usuario ".$this->data['Users']['username'], true), 'flash', array('class' => 'muted'));
		}
		else
		{		
			$errors = $this->Users->validationErrors;
			$this->Session->setFlash(__("No se pudo crear nuevo usuario, por favor revisar los campos requeridos.", true), 'flash_error', array('class' => 'alert alert-error'));
		}
		$this->redirect(array('action' => 'index'));			
	}
}
